<?php
/**
 * 分类管理
 * Created by PhpStorm.
 * User: Administrator
 * Date: 15-6-17
 * Time: 下午2:37
 */
namespace app\modules\cn\controllers;

use app\libs\IeltsController;
use app\modules\content\models\Classes;
use app\modules\content\models\Teachers;
use app\modules\content\models\Evaluation;
use app\libs\Method;

class CourseController extends IeltsController  {
    public $enableCsrfValidation = false;
    public $layout = 'cn';

    public function actionIndex()
    {
        $cate = \Yii::$app->request->get('cate',0);
        $subject = \Yii::$app->request->get('subject',0);
        $score = \Yii::$app->request->get('score',0);
        $page = \Yii::$app->request->get('page',1);
        $where='1=1';
        if($cate!=false){
            $where .=' and cate='.$cate;
        }
        if($subject!=false){
            $where .=' and subject='.$subject;
        }
        if($score!=false){
            $where .=' and score='.$score;
        }
        if($cate!=false || $subject!=false ||$score!=false){
            $offset = 12 * ($page - 1);
            $class [0]= Classes::find()->asArray()->where($where)->limit(12)->offset($offset)->orderBy('sort asc,id desc')->all();//公开课
            $count =count(Classes::find()->asArray()->select('id')->where($where)->orderBy('sort asc,id desc')->all());
//            $url = "/mall/$cate/$subject/$score/$page";
//            $count = $count['count'];
//            $page = new Pager ("$url", $count, $page, 12);
//            $str = $page->GetPager();
            return $this->render('index',['class'=>$class,'count'=>$count]);
        }else{
            $class[1] = Classes::find()->asArray()->where('cate=1 and classesPort=1')->orderBy('sort asc,id desc')->all();//公开课
            $class[2] = Classes::find()->asArray()->where('cate=2 and classesPort=1')->orderBy('sort asc,id desc')->all();//小班课
            $class[3] = Classes::find()->asArray()->where('cate=3 and classesPort=1')->orderBy('sort asc,id desc')->all();//单项课
            $class[4] = Classes::find()->asArray()->where('cate=4 and classesPort=1')->orderBy('sort asc,id desc')->all();//录播课
            $class[5] = Classes::find()->asArray()->where('cate=5 and classesPort=1')->orderBy('sort asc,id desc')->all();//直播课
            $class[6] = Classes::find()->asArray()->where('cate=6 and classesPort=1')->orderBy('sort asc,id desc')->all();//1对1
            $class[7] = Classes::find()->asArray()->where('cate=7 and classesPort=1')->orderBy('sort asc,id desc')->all();//面授课
            $class[8] = Classes::find()->asArray()->where('cate=8 and classesPort=1')->orderBy('sort asc,id desc')->all();//热门课程
            $class[9] = Classes::find()->asArray()->where('cate=9 and classesPort=1')->orderBy('sort asc,id desc')->all();//寒暑假封闭班
            $this->title = '雅思学习在线交易服务平台_写作,口语,阅读,听力,雅思学习服务-申友雅思商城';
            $this->keywords = '雅思在线培训班,雅思写作,雅思口语,雅思听力,雅思阅读,申友雅思商城';
            $this->description = '申友雅思商城是国内领先的出国留学语言培训服务机构，提供雅思写作，雅思口语，雅思阅读，雅思听力等雅思周边产品的交易服务、咨询服务！';
            return $this->render('index',['class'=>$class,'count'=>0]);
        }
    }

    public function actionDetail()
    {
        $id = \Yii::$app->request->get('id');
        $class=Classes::find()->asArray()->where('id='.$id)->one();
        $class['systemPeople']= explode(PHP_EOL,$class['systemPeople']);
        $class['systemContent']= explode(PHP_EOL,$class['systemContent']);
        $class['systemCourse']= explode(PHP_EOL,$class['systemCourse']);
        $class['systemServe']= explode(PHP_EOL,$class['systemServe']);
        if($class['systemTeacher']){
            $str = $class['systemTeacher'];
            $class['systemTeacher'] = Teachers::find()->where("id in ($str)")->asArray()->all();
        }else{
            $class['systemTeacher']=[];
        }
        // var_dump($class);die;
        if($class!=false){
            Classes::updateAll(['viewCount' => ($class['viewCount'] + 1)], "id=$id");
        }
        $model=new Teachers();
        $class['teachers'] =$model->getTeachers($class['teacher']);
        // 处理视频sdk
        $res = json_decode(Method::post('https://live.thinkwithu.com/api/play/pc-video',['word'=>$class['url']]), true);
        if ($res['code'] == 1) {
            $class['url'] = $res['data']['video'];
        }
        // echo "<pre>";var_dump($class);die();
        unset($class['teacher']);
        $recommend=Classes::find()->asArray()->orderBy('viewCount desc')->limit(4)->all();
        $this->title = isset($class['name'])?$class['name'].'-申友雅思':'';
        $this->keywords = isset($class['keywords'])?$class['keywords']:'';
        $this->description = isset($class['summary'])?$class['summary']:'';
        return $this->render('detail',['class'=>$class,'recommend'=>$recommend,'id'=>$id]);
    }

    /**
     * @Title  [课程体系单页]
     * @Author Don
     * @Date   2021-04-12
     * @return [type]
     */
    public function actionSystem()
    {


        $banner =array();
        // 老师列表
        $model = new Teachers();
        $teachers = $model->getTeacherList(3);
        if ($teachers) {
            foreach ($teachers as $k => $v) {
                if ($v['url']) {
                    $res = json_decode(Method::post('https://live.thinkwithu.com/api/play/pc-video',['word'=>$v['url']]), true);
                    if ($res['code'] == 1) {
                        $teachers[$k]['url'] = $res['data']['video'];
                    }
                }
            }
        }
        // echo "<pre>";var_dump($teachers);die();
        $other_teachers = [];
        if ($teachers) {
            $tid = $teachers[0]['id'];
            $other_teachers = $model->getOtherTeacher($tid);
        }
        // 学员评价
        $evaluation_mode = new Evaluation();
        $evaluations = $evaluation_mode->getEvaluationList(3);
        $this->title = '申友雅思在线_课程体系_课程培训';
        $this->keywords = '雅思培训,雅思考试,雅思备考';
        $this->description = '申友雅思为考生提供课程方案，课程服务，学习流程，满足考生各阶段备考需求，只为雅思提分，申友雅思在线';

        return $this->render('course_system', ['banner'=>$banner,'teachers'=>$teachers, 'other_teachers'=>$other_teachers, 'evaluations'=>$evaluations]);
    }

    /**
     * @Title  [取其他三位老师信息]
     * @Author Don
     * @Date   2021-04-13
     * @return [type]
     */
    public function actionGetOtherTeacher()
    {
        $tid = \Yii::$app->request->get('tid');
        $model = new Teachers();
        $data = $model->getOtherTeacher($tid);
        if ($data) {
            $res = [
                'code' => 1,
                'data' => $data,
                'message' => 'success'
            ];
        } else {
            $res = [
                'code' => 0,
                'data' => [],
                'message' => '未找到老师信息'
            ];
        }
        die(json_encode($res));
    }
}